package com.learn.core.controller.learn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.domain.view.UserDetailsGather;
import com.learn.core.dto.LEvaluateDTO;
import com.learn.core.service.*;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName EvaluateController
 * @date 2021.04.08 10:52
 * @describe 课程评论
 */
@Api("课程评论")
@RestController
@RequestMapping("/learn/evaluate")
public class EvaluateController extends WebController<LEvaluate> {

    @Autowired
    private ILEvaluateService ilEvaluateService;
    @Autowired
    private ILUserService ilUserService;
    @Autowired
    private ILPraiseService ilPraiseService;
    @Autowired
    private IUserDetailsGatherService iUserDetailsGatherService;

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //课程评论列表
    @ApiOperation(value = "课程评论列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LEvaluate> list(@RequestBody LEvaluateDTO lEvaluateDTO, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lEvaluateDTO==null||lEvaluateDTO.getPage()==null||lEvaluateDTO.getRows()==null){
                return new PageResult<LEvaluate>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lEvaluateDTO.getPage(),lEvaluateDTO.getRows());
            QueryWrapper<LEvaluate> queryWrapper = new QueryWrapper<LEvaluate>();
            if(StringUtils.isNotEmpty(lEvaluateDTO.getCourseId()))
                queryWrapper.eq("course_id",lEvaluateDTO.getCourseId());
            if(StringUtils.isNotEmpty(lEvaluateDTO.getEvaluateType()))
                queryWrapper.eq("evaluate_type",lEvaluateDTO.getEvaluateType());
            if(StringUtils.isNotEmpty(lEvaluateDTO.getUserId()))
                queryWrapper.eq("user_id",lEvaluateDTO.getUserId());
            Page<LEvaluate> lEvaluates = (Page<LEvaluate>) ilEvaluateService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lEvaluates)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LEvaluate lEvaluate : lEvaluates) {
                QueryWrapper<UserDetailsGather> userDetailsGatherQueryWrapper = new QueryWrapper<UserDetailsGather>();
                userDetailsGatherQueryWrapper.eq("user_id",lEvaluate.getUserId());
                UserDetailsGather lUser = iUserDetailsGatherService.getOne(userDetailsGatherQueryWrapper);
                if(null != lUser){
                    lEvaluate.setUser(lUser);
                }
                QueryWrapper<LPraise> lPraiseQueryWrapper = new QueryWrapper<LPraise>();
                lPraiseQueryWrapper.eq("user_id",userId);
                lPraiseQueryWrapper.eq("course_id",lEvaluate.getCourseId());
                LPraise lPraise = ilPraiseService.getOne(lPraiseQueryWrapper);
                if(null != lPraise){
                    lEvaluate.setPraiseState("1");
                }else{
                    lEvaluate.setPraiseState("0");
                }
            }
            //好评率
            QueryWrapper<LEvaluate> lEvaluateQueryWrapper = new QueryWrapper<LEvaluate>();
            lEvaluateQueryWrapper.eq("course_id",lEvaluateDTO.getCourseId());
            int allCount = ilEvaluateService.count(lEvaluateQueryWrapper);
            lEvaluateQueryWrapper = new QueryWrapper<LEvaluate>();
            lEvaluateQueryWrapper.eq("course_id",lEvaluateDTO.getCourseId());
            lEvaluateQueryWrapper.eq("evaluate_type","1");
            int evaluateType = ilEvaluateService.count(lEvaluateQueryWrapper);
            // 返回
            return new PageResult(lEvaluates.getTotal(), lEvaluates, ResultEnum.SUCCESS, (int) (evaluateType *100.00 / allCount));
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加课程评论", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LEvaluate lEvaluate, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lEvaluate!=null) {
                if (StringUtils.isEmpty(lEvaluate.getCourseId()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSE_ID);
                if (StringUtils.isEmpty(lEvaluate.getUserId()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_USER_ID);
                lEvaluate.setCreateTime(new Date());
                lEvaluate.setCreateUser(userId);
                if (ilEvaluateService.save(lEvaluate)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除课程评论", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LEvaluate lEvaluate){
        try {
            if(lEvaluate!=null){
                if(ilEvaluateService.removeById(lEvaluate.getEvaluateId())){
                    return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e) {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新课程评论", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LEvaluate lEvaluate, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lEvaluate!=null){
            lEvaluate.setUpdateTime(new Date());
            lEvaluate.setUpdateUser(userId);
            if(ilEvaluateService.updateById(lEvaluate)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
